package cn.tedu.egarticle.controller;

import cn.tedu.egarticle.base.response.JsonResult;
import cn.tedu.egarticle.base.response.StatusCode;
import cn.tedu.egarticle.mapper.ArticleMapper;
import cn.tedu.egarticle.mapper.UserMapper;
import cn.tedu.egarticle.pojo.dto.UserArticleParam;
import cn.tedu.egarticle.pojo.dto.UserLoginParam;
import cn.tedu.egarticle.pojo.entity.Article;
import cn.tedu.egarticle.pojo.entity.User;
import cn.tedu.egarticle.pojo.vo.ArticleVO;
import cn.tedu.egarticle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.annotation.HttpConstraint;
import javax.servlet.http.HttpSession;

@Slf4j
@Validated
@Api(tags = "01.用户管理模块")
@RestController()
@RequestMapping("v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("login")
    @ApiOperation("登录功能")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("userLoginParam = " + userLoginParam);
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user", userVO);
        return JsonResult.ok(userVO);
    }
    @Autowired
    private ArticleMapper articleMapper;
    @ApiOperation(value = "发表文章")
    @ApiOperationSupport(order = 20)
    @PostMapping("article")
    public JsonResult addArticle(@Validated @RequestBody UserArticleParam userArticleParam,
                              @ApiIgnore HttpSession session){
        log.debug("userArticleParam = " + userArticleParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Article article = new Article();
        BeanUtils.copyProperties(userArticleParam,article);
        article.setUserId(userVO.getId());
        int i = userMapper.insertArticle(article);
        if(i == 0){
            return new JsonResult(StatusCode.VALIDATED_ERROR);
        }
        return JsonResult.ok(userArticleParam);
    }
    @GetMapping("selectByArticleId")
    @ApiOperation("根据文章id查询内容")
    @ApiOperationSupport(order = 30)
    public JsonResult selectByArticleId(@Range(min = 1, max = 100, message = "1-100") @RequestParam Long id, @ApiIgnore HttpSession session){
        log.debug("id = " + id);
        ArticleVO articleVO = userMapper.selectByArticleId(id);
        return JsonResult.ok(articleVO);
    }
}
